package com.b.service.impl;

import com.b.repository.UserAuthRepository;
import com.b.vo.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import com.b.dao.UserAuthMapper;
import com.b.domain.UserAuth;
import com.b.service.UserAuthService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserAuthServiceImpl implements UserAuthService{

    @Resource
    private UserAuthMapper userAuthMapper;
    @Resource
    private UserAuthRepository userAuthRepository;

    @Override
    public int deleteByPrimaryKey(Integer userId) {
        return userAuthMapper.deleteByPrimaryKey(userId);
    }

    @Override
    public int insert(UserAuth record) {
        return userAuthMapper.insert(record);
    }

    @Override
    public int insertSelective(UserAuth record) {
        return userAuthMapper.insertSelective(record);
    }

    @Override
    public UserAuth selectByPrimaryKey(Integer userId) {
        return userAuthMapper.selectByPrimaryKey(userId);
    }

    @Override
    public int updateByPrimaryKeySelective(UserAuth record) {
        return userAuthMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(UserAuth record) {
        return userAuthMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageInfo<UserAuth> userList(String phone, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<UserAuth> list = userAuthMapper.userList(phone);
        return new PageInfo<>(list);
    }

    @Override
    public List<UserAuth> getAll() {
        return userAuthMapper.getAll();
    }

    @Override
    public SearchPage<UserAuth> findUserAuthByNameLikeAndPhoneLike(PageRequest pageRequest, String name, String phone) {
        return userAuthRepository.findUserAuthByNameLikeOrPhoneLike(pageRequest, name, phone);
    }

    @Override
    @Transactional
    public Result banUser(Integer userId) {
        try {
            // 1. 先查es
            UserAuth userAuth = userAuthRepository.findByUserId(userId);
            if (userAuth == null) {
                return new Result().setCode("400").setMsg("用户不存在");
            } else {

                // 2. 修改状态
                if ("封禁".equals(userAuth.getStatus())) {
                    userAuth.setStatus("正常");
                } else {
                    userAuth.setStatus("封禁");
                }
                // 3. 更新数据库
                int updateDb = userAuthMapper.updateByPrimaryKeySelective(userAuth);
                // 4. 同步到ES
                userAuthRepository.save(userAuth);
                if (updateDb > 0) {
                    return new Result().setCode("200").setMsg("操作成功");
                } else {
                    return new Result().setCode("400").setMsg("操作失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result().setCode("500").setMsg("服务器错误");
        }
    }

}
